﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using System.Windows.Forms;

namespace CountryList
{
	public partial class MainGUI : Form
	{
		/// <summary>
		/// 国家字段名
		/// </summary>
		public static readonly string COUNTRY_FIELD_NAME = "CNTRY_NAME";

		/// <summary>
		/// 全球国家图层
		/// </summary>
		private IFeatureLayer countryLayer;

		public MainGUI()
		{
			InitializeComponent();
			// 设置地图名
			mainMap.Map.Name = "Map";
			// 获取国家图层
			countryLayer = (IFeatureLayer)mainMap.ActiveView.FocusMap.Layer[0];
			// 获取国家图层属性表
			ITable table = (ITable)countryLayer.FeatureClass;
			// 找到国家名字段的位置（列数）
			int index = table.FindField(COUNTRY_FIELD_NAME);
			for (int i = 0; i < table.RowCount(null); i++)
			{
				// 获取每一行的国家名的值
				comboBox.Items.Add(table.GetRow(i).Value[index]);
			}
			comboBox.SelectedIndex = 0;
		}

		private void highlight_Click(object sender, System.EventArgs e)
		{
			if (comboBox.Items.Count == 0)
			{
				return;
			}
			// 新建查询器
			IQueryFilter filter = new QueryFilterClass();
			// 设定查询条件
			filter.WhereClause = COUNTRY_FIELD_NAME + "=" + "\'" + comboBox.SelectedItem.ToString() + "\'";
			// 新建游标，根据查询条件指向指定要素
			IFeatureCursor cursor = countryLayer.Search(filter, true);
			// 获取要素
			IFeature feature = cursor.NextFeature();
			// 闪烁图层
			mainMap.FlashShape(feature.Shape);
			cursor.Flush();
		}
	}
}